package com.itheima.a01mymap;

import java.util.StringJoiner;
import java.util.TreeMap;

public class A04_TreeMapDemo3 {
    public static void main(String[] args) {
        //定义一个字符串
        String str="asnibbbbbhaaaaaddbmdddddsamk";

        //定义一个集合,按照值的大小来排序
        TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();

        //获取字符串里面每一个字符
        for (int i = 0; i < str.length(); i++) {
            char key = str.charAt(i);
            //在把每一个字符添加到集合
            if(tm.containsKey(key)){
                //字符存在，值加一，再重新赋值
                //先获取值
                Integer value = tm.get(key);
                //值加1
                value++;
                //重新赋值
                tm.put(key,value);
            }else{
                //不存在，直接添加
                tm.put(key,1);
            }
        }

        //创建StringBuilder用于拼接字符串
        StringBuilder sb = new StringBuilder();

        //拼接
        tm.forEach((key,value)->sb.append(key+"("+value+")"));

        //创建StringJoiner进行拼接
        StringJoiner sj = new StringJoiner("","","");

        tm.forEach(( key,  value)-> sj.add(key+"("+value+")"));

        //输出
        System.out.println(sb);
        System.out.println(sj);
    }
}
